﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace EarringsRecommendationWeb
{
    public partial class CheckoutOrder : System.Web.UI.Page
    {
        private string _earringsId;
        protected void Page_Load(object sender, EventArgs e)
        {
            Member mem = Session["member"] as Member;

            if (mem != null)
            {
                Response.Write("<input id='hidden-email' type='hidden' value='" + mem.Email + "'>");
                Response.Write("<input id='hidden-name' type='hidden' value='" + mem.Name + "'>");
            }

            List<RecalQty> listQty = Session["RecalQty"] as List<RecalQty>;
            int qtyB = 0;
            if (listQty != null)
            {
                foreach (RecalQty item in listQty)
                {
                    qtyB += Convert.ToInt32(item.Qty);
                }
            }
            ClientScript.RegisterArrayDeclaration("qty", string.Format("\"{0}\"", qtyB));
            

            if (!IsPostBack)
            {
                if (Session["RecalQty"] != null)
                {
                    List<RecalQty> lstRecal = Session["RecalQty"] as List<RecalQty>;
                    //Check ReceieveDate
                    string[] receiveDate = GlobalFunction.GetReceiveDate(lstRecal);

                    string str = string.Empty;
                    foreach (RecalQty item in lstRecal)
                    {
                        if (string.IsNullOrEmpty(str))
                        {
                            str += "'" + item.ERCode + "'";
                        }
                        else
                        {
                            str += ",'" + item.ERCode + "'";
                        }
                    }
                    _earringsId = str;
                    //Sample Data
                    //_earringsId = "('SD-0002','CT-0003')";
                    if (!string.IsNullOrEmpty(_earringsId))
                    {  
                        DataTable dtEarringsResult = GlobalFunction.GetEarringsInOrder(_earringsId); ;

                        foreach (DataRow row in dtEarringsResult.Rows)
                        {
                            if (row["er_remaining"] != null) 
                            {
                                int remain = Convert.ToInt32(row["er_remaining"].ToString());
                                //Check Remaining
                                string status = string.Empty;
                                if (remain <= 0)
                                {
                                    status = GlobalValue.ERSTATUS_WAIT;
                                }
                                else
                                {
                                    status = GlobalValue.ERSTATUS_READY;
                                }

                                row["er_status"] = status;
                            }
                        }
                        string expression;
                        string qty;
                        foreach (RecalQty item in lstRecal)
                        {
                            qty = item.Qty;
                            expression = "er_code ='" + item.ERCode + "'" ;
                            DataRow[] foundRows;

                            // Use the Select method to find all rows matching the filter.
                            foundRows = dtEarringsResult.Select(expression);
                            foundRows[0]["er_qty"] = qty;
                        }

                        ClientScript.RegisterArrayDeclaration("EarringsInOrder", JsonConvert.SerializeObject(dtEarringsResult));

                        ClientScript.RegisterArrayDeclaration("ReceiveDate", JsonConvert.SerializeObject(receiveDate));

                        DataTable dtMemberInfo = this.GetMemberInfo();
                        ClientScript.RegisterArrayDeclaration("Member", JsonConvert.SerializeObject(dtMemberInfo));

                    }
                }
            }
        }      

        private DataTable GetMemberInfo()
        {
            DataTable dtResult = null;

            Member mem = Session["member"] as Member;
            string email = string.Empty;
            if (mem != null)
            {
                email = mem.Email;

                ////SampleData
                //email = "sukanya.manee@gmail.com";

                SqlParameter[] sqlParams = new SqlParameter[] 
                {
                    new SqlParameter("@Email", email)
                };

                string sqlStr = string.Empty;
                sqlStr = "SELECT Email, MemberName AS [Name], [Address] FROM Member WHERE Email = @Email";

                dtResult = SqlClientUtility.ExecuteDataTable(CommandType.Text, sqlStr.ToString(), sqlParams);
                
            }
            return dtResult;
        }
    }
}